package com.example.pengxiangedupay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pengxiangedupay.common.constant.OrderStatus;
import com.example.pengxiangedupay.common.utils.FeeUtils;
import com.example.pengxiangedupay.common.utils.OrderNoUtils;
import com.example.pengxiangedupay.dao.OrderInfoDao;
import com.example.pengxiangedupay.entity.FeeStuNeedPayEntity;
import com.example.pengxiangedupay.entity.FeeStuPaidEntity;
import com.example.pengxiangedupay.entity.OrderInfoEntity;
import com.example.pengxiangedupay.service.FeeStuNeedPayService;
import com.example.pengxiangedupay.service.FeeStuPaidService;
import com.example.pengxiangedupay.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoDao,OrderInfoEntity> implements OrderInfoService {

    @Autowired
    FeeStuPaidService feeStuPaidService;
    @Autowired
    FeeStuNeedPayService feeStuNeedPayService;
    @Override
    public OrderInfoEntity createOrderByProductId(Long productId,BigDecimal total) {
        //查找已存在但未支付的订单
//        OrderInfoEntity orderInfo = this.getNoPayOrderByProductId(productId);
//        if(orderInfo != null) {
//            return orderInfo;
//        }
        //获取商品信息
        OrderInfoEntity orderInfo = new OrderInfoEntity();
        orderInfo.setTitle(getProductTitleById(productId));
        orderInfo.setUserId(feeStuPaidService.getById(productId).getStuId());
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
        orderInfo.setProductId(productId);
        //设置总金额 有bug
        orderInfo.setTotalFee(total);
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        baseMapper.insert(orderInfo);
        return orderInfo;
    }

    @Override
    public OrderInfoEntity getNoPayOrderByProductId(Long productId) {
        OrderInfoEntity orderInfo = this.getOne(new QueryWrapper<OrderInfoEntity>().eq("order_status", OrderStatus.NOTPAY.getType()).eq("product_id",productId));
        return orderInfo;
    }

    @Override
    public OrderInfoEntity getOrderByOrderNo(String outTradeNo) {

        return this.baseMapper.selectOne(new QueryWrapper<OrderInfoEntity>().eq("order_no",outTradeNo));
    }

    @Override
    public String getOrderStatus(String orderNo) {

        QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfoEntity orderInfo = baseMapper.selectOne(queryWrapper);
        if(orderInfo == null){
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {

        log.info("更新订单状态 ===> {}", orderStatus.getType());

        QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        OrderInfoEntity orderInfo = new OrderInfoEntity();
        orderInfo.setOrderStatus(orderStatus.getType());

        baseMapper.update(orderInfo, queryWrapper);
    }

    private String getProductTitleById(Long productId){
        FeeStuPaidEntity feeStuPaid = feeStuPaidService.getById(productId);
        return "学费：" + feeStuPaid.getBillInfo();
    }
    private BigDecimal getTotalFeeById(Long productId){
        FeeStuPaidEntity feeStuPaid = feeStuPaidService.getById(productId);
        FeeStuNeedPayEntity feeNeedPay = feeStuNeedPayService.getById(feeStuPaid.getFeeNeedPayId());
        BigDecimal total = feeNeedPay.getTotalNeedFee().subtract(feeStuPaid.getTotalPaidFee());
        return total;
    }
}
